<?php
	/**
	 * @file validateCode Create Class
	 * @author haifeng
	 * @time 20140612
	 */
	class Validatecode{
		//validatecode charset
		private $_charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
		private $_code;
		private $_codelen;
		private $_width;
		private $_height;
		private $_im;
		private $_font;
		private $_fontcolor;
		private $_fontsize;
		
		/**
		 * php 5 construct
		 * @param int $codelen validatecode length defualt 6
		 * @param int $width validatecode width defualt 130
		 * @param int $height validatecode height defualt 50
		 * @param int $fontsize validatecode font size defualt 18
		 */
		public function __construct($codelen = 4, $width = 90, $height = 40, $fontsize = 17){
				$this->_codelen = is_numeric($codelen) ? $codelen : 4;
				$this->_width = is_numeric($width) ? $width : 90;
				$this->_height = is_numeric($height) ? $height : 40;
				$this->_fontsize = is_numeric($fontsize) ? $fontsize : 17;
				$this->_font = PUBLICPATH.'/font/tahoma.ttf';
		}
		
		/**
		 * Create validate code 
		 * private
		 */
		private function _createCode(){
			$charlen = strlen($this->_charset) - 1;
			for($i=0;$i<$this->_codelen;$i++){
				$this->_code .= $this->_charset[mt_rand(0,$charlen)];
			}
			 return $this;
		}
		
		/**
		 * Create validatecode bg
		 * private
		 */
		private function _createBg(){
			//创建一个真彩色图像 返回图像标识符
			$this->_im = imagecreatetruecolor($this->_width, $this->_height);
			//为图像分配颜色 防止图片颜色和字体颜色相同 分配图像2/3颜色区域 字体1/3区域
  			$color = imagecolorallocate($this->_im, mt_rand(86,255), mt_rand(86,255), mt_rand(86,255));
			//画一矩形并以$color填充
  			imagefilledrectangle($this->_im,0,$this->_height,$this->_width,0,$color);
			 return $this;
		}
		
		/**
		 * Create validatecode font
		 * private
		 */
		private function _createFont(){
			$distanceoffice = $this->_width / $this->_codelen;//以图片宽度/文字数目 + 5 作为文字插入点
			for($i = 0;$i<$this->_codelen;$i++){
				//为文字分配颜色
				$this->_fontcolor = imagecolorallocate($this->_im,mt_rand(0,85),mt_rand(0,85),mt_rand(0,85));
				 imagettftext($this->_im,$this->_fontsize,mt_rand(-30,30),$distanceoffice * $i+mt_rand(0,5),$this->_height / 1.4,$this->_fontcolor,$this->_font,$this->_code[$i]);
			}
			 return $this;
		}
		
		/**
		 * Create validatecode line
		 * private
		 */
		private function _createLine(){
			 $jam = '.*_`~';
			 //线条
			 for($i=0;$i<3;$i++){
			 	$color = imagecolorallocate($this->_im,mt_rand(0,85),mt_rand(0,85),mt_rand(0,85));
				 imageline($this->_im,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color);
			 }
			 //雪花
			 for($i;$i<30;$i++){
			 	$color = imagecolorallocate($this->_im,mt_rand(0,85),mt_rand(0,85),mt_rand(0,85));
				imagestring($this->_im,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$jam[mt_rand(0,4)],$color);
			 }
			 return $this;
		 }
		 
		/**
		 * Output
		 * private
		 */
		private function _outPut(){
			header('Content-Type:image/png');
  			imagepng($this->_im);
  			imagedestroy($this->_im);
			return $this;
		}
		
		/**
		 * out output
		 */
		public function doImg(){
			$this->_createBg()->_createCode()->_createLine()->_createFont()->_outPut()->_setSessionCode();
		}
		
		/**
		 * get validate code 
		 */
		private function _setSessionCode(){
			Session::setValue('validatecode',$this->_code);
		}
	}
?>